﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace AssignmentsManage.Windows
{
    /// <summary>
    /// Interaction logic for CourseWindow.xaml
    /// </summary>
    public partial class CourseWindow : Window, INotifyPropertyChanged
    {
        public CourseWindow()
        {
            InitializeComponent();
        }

        #region Properties

        private ObservableCollection<DataClasses.ActivityTypeWorth> _ActivityWorths;
        public ObservableCollection<DataClasses.ActivityTypeWorth> ActivityWorths
        {
            get
            {
                return _ActivityWorths;
            }
            set
            {
                _ActivityWorths = value;
                OnPropertyChanged("ActivityWorths");
            }
        }

        private ObservableCollection<SetCLib.TitleValueXML> _CustomProperties;
        public ObservableCollection<SetCLib.TitleValueXML> CustomProperties
        {
            get
            {
                return _CustomProperties;
            }
            set
            {
                _CustomProperties = value;
                OnPropertyChanged("CustomProperties");
            }
        }

        #endregion

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                this.DialogResult = true;
                this.Close();
            }
        }

        private bool Validate()
        {
            double totalPercent = 0;
            foreach(DataClasses.ActivityTypeWorth worth in ActivityWorths)
            {
                totalPercent += worth.Worth;
            }
            if (totalPercent > 0 && totalPercent < 100)
            {
                if (MessageBox.Show("The total combined percent should equal 100% for grade distribution to work. If it is less than 100% the program will assume all grades are worth their exact values.\nIf you do not want to use grade distribution you should leave all at 0%", "Grade Distribution Warning", MessageBoxButton.OKCancel, MessageBoxImage.Warning)
                    == MessageBoxResult.Cancel)
                {
                    return false;
                }
            }
            else if (totalPercent > 100)
            {
                MessageBox.Show("The total combined percent for grade distributions cannot be larger than 100%", "Grade Distribution Error", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                return false;
            }

            return true;
        }

        #region NotifyChanges

        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Grade Distribution

        private void DecimalUpDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            double count = 0;

            foreach(DataClasses.ActivityTypeWorth worth in ActivityWorths)
            {
                count += worth.Worth;
            }

            Converters.NumberToPercentStringConverter conv1 = new Converters.NumberToPercentStringConverter();
            if (count != 100 && count != 0)
            {
                runTotalGradeDist.Foreground = new SolidColorBrush(Colors.Red);
            }
            else
            {
                runTotalGradeDist.Foreground = new SolidColorBrush(Colors.Green);
            }
            runTotalGradeDist.Text = conv1.Convert(count, typeof(string), string.Empty, null).ToString();
        }

        #endregion

        #region Custom Properties

        private void txtExtraTitle_TextChanged(object sender, TextChangedEventArgs e)
        {
            SetCLib.TitleValueXML currentItem = ((SetCLib.TitleValueXML)((TextBox)sender).DataContext);
            int added = ((TextBox)sender).Text.Length - e.Changes.ToList<TextChange>()[0].AddedLength;
            int indexOn = CustomProperties.IndexOf(currentItem);

            if (!String.IsNullOrEmpty(((TextBox)sender).Text) &&
                indexOn == CustomProperties.Count - 1 &&
                added == 0 &&
                String.IsNullOrEmpty(currentItem.Value))
            {
                CustomProperties.Add(new SetCLib.TitleValueXML());
            }

            if (String.IsNullOrEmpty(((TextBox)sender).Text))
            {
                currentItem.Title = null;
            }
        }

        private void txtExtraValue_TextChanged(object sender, TextChangedEventArgs e)
        {
            SetCLib.TitleValueXML currentItem = ((SetCLib.TitleValueXML)((TextBox)sender).DataContext);
            int added = ((TextBox)sender).Text.Length - e.Changes.ToList<TextChange>()[0].AddedLength;
            int indexOn = CustomProperties.IndexOf(currentItem);

            if (!String.IsNullOrEmpty(((TextBox)sender).Text) &&
                indexOn == CustomProperties.Count - 1 &&
                added == 0 &&
                String.IsNullOrEmpty(currentItem.Title))
            {
                CustomProperties.Add(new SetCLib.TitleValueXML());
            }

            if (String.IsNullOrEmpty(((TextBox)sender).Text))
            {
                currentItem.Value = null;
            }
        }

        /// <summary>
        /// Button click to delete the associated extra property
        /// </summary>
        private void btnDeleteInfo_Click(object sender, RoutedEventArgs e)
        {
            SetCLib.TitleValueXML currentItem = (SetCLib.TitleValueXML)((Button)sender).DataContext;
            CustomProperties.Remove(currentItem);
        }

        #endregion

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //Grade Distribution
            DecimalUpDown_ValueChanged(sender, null);

            //Custom Properties
            CustomProperties.Add(new SetCLib.TitleValueXML());

            //Schedule

        }
    }
}
